package com.mr.cai.main.view;


import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mr.cai.main.R;

public class HorizontallyView extends HorizontalScrollView implements OnClickListener{
	private FrameLayout frameLy;
	private SparseArray<View> mSparseView;
	private onSelecterListener selecterListener;
	private BaseAdapter mBaseAdapter;
	private int oldPos;
	private TextView textView;
	private boolean initLoadAnim;
	private Context mContext;
	public HorizontallyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext=context;
		initView();
	}
	public HorizontallyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext=context;
		initView();
	}
	public HorizontallyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext=context;
		initView();
	}
	public interface onSelecterListener{
		void onSelect(int index);
	}; 
	public void setOnSelecterListener(onSelecterListener selecterListener) {
		this.selecterListener = selecterListener;
	
	}
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		super.onScrollChanged(l, t, oldl, oldt);
		
	}

	private void initView() {
		// TODO Auto-generated method stub
		frameLy=new FrameLayout(getContext());
		frameLy.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		addView(frameLy);
		mSparseView=new SparseArray<>();
		setFocusable(true);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		if(initLoadAnim){
			initAnim();
			initLoadAnim=false;
		}
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(selecterListener==null){
			return;
		}
		
		int position=mSparseView.indexOfValue(v);
		selecterListener.onSelect(position);
	}

	public void initAnim(){
		int position=0;
		startAnimation(position);
	}
	public void setAdapter(BaseAdapter baseAdapter){
		if(baseAdapter==null){
			return;
		}
		mBaseAdapter=baseAdapter;
		mBaseAdapter.registerDataSetObserver(new DataSetObserver() {
			@Override
			public void onChanged() {
				// TODO Auto-generated method stub
				oldPos=0;
				buildContentView();
				super.onChanged();
			}
		});
		mBaseAdapter.notifyDataSetChanged();
		initLoadAnim=true;
	}

	private void startAnimation(int position) {
		// TODO Auto-generated method stub
		final AnimationSet set=new AnimationSet(true);
		set.addAnimation(buildScaleAnimation(oldPos, position));
		set.addAnimation(buildTranslateAnimation(oldPos, position));

		set.setInterpolator(new AccelerateDecelerateInterpolator());
		set.setFillAfter(true);
		set.setDuration(350);
//		textView.setAnimation(set);
		textView.startAnimation(set);

		smoothScrollTo((getItemView(position).getWidth() * position - 90), 0);
		int j=mSparseView.size();
		for(int i=0;i<j;i++){
			if(i==position){
				TextView textVv=(TextView) getItemView(i);
				textVv.setTextColor(mContext.getResources().getColor(R.color.color_main_green));
			}else{
				TextView textVv=(TextView) getItemView(i);
				textVv.setTextColor(mContext.getResources().getColor(R.color.color_black));
			}		
		}
		
		invalidate();
	}
	public void clearTvAnimation(){
		textView.clearAnimation();
	}
	public void startAnim(int position){
		startAnimation(position);
		oldPos=position;
	}

	private Animation buildScaleAnimation(int oldPos,int newPos){
		float oldWidth=getItemView(oldPos).getWidth();
		float newWidth=getItemView(newPos).getWidth();
		float fromX=oldWidth/textView.getWidth();
		float toX=newWidth/textView.getWidth();
		ScaleAnimation anim=new ScaleAnimation(fromX, toX, 1f, 1f,Animation.ABSOLUTE,0.0f,Animation.ABSOLUTE,0.0f);
		return anim;
	}
	private Animation buildTranslateAnimation(int oldPos,int newPos){
		TranslateAnimation anim=new TranslateAnimation(getItemView(oldPos).getLeft(), getItemView(newPos).getLeft(), 0, 0);
		return anim;
	}
	private View getItemView(int position){
		return mSparseView.get(position);
	}
	private void buildContentView(){
		LinearLayout linear=new LinearLayout(getContext());
		int j=mBaseAdapter.getCount();
		for(int i=0;i<j;i++){
			View view=mBaseAdapter.getView(i, mSparseView.get(i), this);
			view.setOnClickListener(this);
			mSparseView.put(i, view);
			linear.addView(mSparseView.get(i));
		}
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		layoutParams.gravity= Gravity.CENTER_VERTICAL;
		textView=(TextView) LayoutInflater.from(getContext()).inflate(R.layout.navigation_item,null);
//		textView.setBackgroundResource(R.drawable.bg_view);
		frameLy.addView(textView, layoutParams);
		frameLy.addView(linear);
		
	}
}
